package org.jeecg.modules.nurse_user_orders_refund.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.base.BaseMap;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.nurse_user_orders.entity.NurseUserOrders;
import org.jeecg.modules.nurse_user_orders.service.INurseUserOrdersService;
import org.jeecg.modules.nurse_user_orders_refund.entity.NurseUserOrdersRefund;
import org.jeecg.modules.nurse_user_orders_refund.service.INurseUserOrdersRefundService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.websocket.NurseWebSocket;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 用户订单退款申请
 * @Author: jeecg-boot
 * @Date: 2024-06-14
 * @Version: V1.0
 */
@Api(tags = "用户订单退款申请")
@RestController
@RequestMapping("/nurse_user_orders_refund/nurseUserOrdersRefund")
@Slf4j
public class NurseUserOrdersRefundController extends JeecgController<NurseUserOrdersRefund, INurseUserOrdersRefundService> {
    @Autowired
    private INurseUserOrdersRefundService nurseUserOrdersRefundService;

    @Autowired
    private INurseUserOrdersService nurseUserOrdersService;


    @Autowired
    private RedisUtil redisUtil;

//    @Autowired
//    private NurseWebSocket nurseWebSocket;

    /**
     * 微信支付用户订单退款
     *
     * @param outTradeNo
     * @return
     */
    @AutoLog(value = "微信用户订单-退款")
    @ApiOperation(value = "微信用户订单-退款", notes = "微信用户订单-退款")
    @GetMapping(value = "/refund")
    public Result<?> refund(@RequestParam(name = "outTradeNo") String outTradeNo) {

        BaseMap map = nurseUserOrdersService.refund(outTradeNo);

        if (oConvertUtils.isNotEmpty(map.get("status"))) {
            NurseUserOrdersRefund nurseUserOrdersRefund = nurseUserOrdersRefundService.getById(outTradeNo);
            if (nurseUserOrdersRefund != null) {
                nurseUserOrdersRefund.setStatus(map.get("status"));
                nurseUserOrdersRefundService.updateById(nurseUserOrdersRefund);
            }
        }

        return Result.OK(map);
    }

    /**
     * 分页列表查询
     *
     * @param nurseUserOrdersRefund
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "用户订单退款申请-分页列表查询")
    @ApiOperation(value = "用户订单退款申请-分页列表查询", notes = "用户订单退款申请-分页列表查询")
    @GetMapping(value = "/list")
    @PermissionData(pageComponent = "nurse/nurse_user_orders_refund/NurseUserOrdersRefundList")
    public Result<IPage<NurseUserOrdersRefund>> queryPageList(NurseUserOrdersRefund nurseUserOrdersRefund,
                                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                              HttpServletRequest req) {
        QueryWrapper<NurseUserOrdersRefund> queryWrapper = QueryGenerator.initQueryWrapper(nurseUserOrdersRefund, req.getParameterMap());
        Page<NurseUserOrdersRefund> page = new Page<NurseUserOrdersRefund>(pageNo, pageSize);
        IPage<NurseUserOrdersRefund> pageList = nurseUserOrdersRefundService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param nurseUserOrdersRefund
     * @return
     */
    @AutoLog(value = "用户订单退款申请-添加")
    @ApiOperation(value = "用户订单退款申请-添加", notes = "用户订单退款申请-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody NurseUserOrdersRefund nurseUserOrdersRefund) {

        String orderId = nurseUserOrdersRefund.getOrderId();
        NurseUserOrders nurseUserOrders = nurseUserOrdersService.getById(orderId);

        if (!nurseUserOrders.getStatus().equals("2") && !nurseUserOrders.getStatus().equals("4")) {
            throw new JeecgBootException("该订单不能进行申请退款!");
        }

        nurseUserOrdersRefund.setId(orderId);
        nurseUserOrdersRefund.setCreateBy(nurseUserOrders.getUserName());
        nurseUserOrdersRefund.setGoodsName(nurseUserOrders.getOrderText());
        nurseUserOrdersRefund.setAmt(nurseUserOrders.getAmt());
        nurseUserOrdersRefund.setNurseType(nurseUserOrders.getNurseType());

        nurseUserOrders.setStatus("8");
        nurseUserOrdersRefund.setStatus("8");
        nurseUserOrdersService.updateById(nurseUserOrders);
        nurseUserOrdersRefundService.save(nurseUserOrdersRefund);

        redisUtil.set("refundOrder", DateUtils.getTimestamp());

//        nurseWebSocket.sendAllMessage(jsonObject.toJSONString());

        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param nurseUserOrdersRefund
     * @return
     */
    @AutoLog(value = "用户订单退款申请-编辑")
    @ApiOperation(value = "用户订单退款申请-编辑", notes = "用户订单退款申请-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody NurseUserOrdersRefund nurseUserOrdersRefund) {
        nurseUserOrdersRefundService.updateById(nurseUserOrdersRefund);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户订单退款申请-通过id删除")
    @ApiOperation(value = "用户订单退款申请-通过id删除", notes = "用户订单退款申请-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        nurseUserOrdersRefundService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "用户订单退款申请-批量删除")
    @ApiOperation(value = "用户订单退款申请-批量删除", notes = "用户订单退款申请-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.nurseUserOrdersRefundService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "用户订单退款申请-通过id查询")
    @ApiOperation(value = "用户订单退款申请-通过id查询", notes = "用户订单退款申请-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<NurseUserOrdersRefund> queryById(@RequestParam(name = "id", required = true) String id) {
        NurseUserOrdersRefund nurseUserOrdersRefund = nurseUserOrdersRefundService.getById(id);
        if (nurseUserOrdersRefund == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(nurseUserOrdersRefund);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param nurseUserOrdersRefund
     */
    @RequiresPermissions("nurse_user_orders_refund:nurse_user_orders_refund:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, NurseUserOrdersRefund nurseUserOrdersRefund) {
        return super.exportXls(request, nurseUserOrdersRefund, NurseUserOrdersRefund.class, "用户订单退款申请");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("nurse_user_orders_refund:nurse_user_orders_refund:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, NurseUserOrdersRefund.class);
    }

}
